/*
package com.lzy.token.manage.impl;

import com.lzy.model.AuthToken;
import com.lzy.model.AuthUser;
import com.lzy.properties.TokenProperties;
import com.lzy.redis.RedisService;
import com.lzy.token.factory.impl.JwtTokenFactoryServiceImpl;
import com.lzy.token.manage.TokenManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

*
 * @author lzy by 2019/9/16 18:38


@Slf4j
@Component
public class TokenManageRedisServiceImpl implements TokenManageService {

    private JwtTokenFactoryServiceImpl jwtTokenFactoryService;

    private RedisService redisService;

    private TokenProperties tokenProperties;

    @Override
    public AuthToken save(String code, String memberCode, Long memberId) {
        AuthUser authUser = new AuthUser();
        authUser.setId(memberId);
        authUser.setCode(code);
        authUser.setMemberCode(memberCode);
        AuthToken token = jwtTokenFactoryService.createAuthToken(authUser);
        redisService.set(this.getAccessTokenKey(token.getAccessToken(), code, memberId), token, tokenProperties.getAccessTokenExpireTime());
        redisService.setToHash(this.getUserTokenKey(code, memberId), token.getAccessToken(), token);
        return token;
    }


    @Override
    public void remove(String token) {
        AuthUser tokenData = jwtTokenFactoryService.getTokenData(token);
        if (tokenData == null) {
            return;
        }
        redisService.delete(this.getAccessTokenKey(token, tokenData.getCode(), tokenData.getId()));

    }

    @Override
    public void removeAll(String token) {
        AuthUser tokenData = jwtTokenFactoryService.getTokenData(token);
        if (tokenData == null) {
            return;
        }
        removeAll(tokenData.getCode(), tokenData.getId());
    }

    @Override
    public void removeAll(String code, Long memberId) {
        Set<String> userToken = userToken(code, memberId);
        redisService.delete(this.getUserTokenKey(code, memberId));
        userToken.forEach(o -> redisService.delete(this.getAccessTokenKey(o, code, memberId)));
    }

    @Override
    public Object getTokenUserDetails(String token) {
        AuthUser tokenData = jwtTokenFactoryService.getTokenData(token);
        if (tokenData == null) {
            return null;
        }
        if (redisService.get(this.getAccessTokenKey(token, tokenData.getCode(), tokenData.getId())) == null) {
            return null;
        }
        return redisService.getFromHash(tokenData.getMemberCode(), tokenData.getId());
    }

    @Override
    public AuthUser getTokenData(String token) {
        AuthUser tokenData = jwtTokenFactoryService.getTokenData(token);
        if (tokenData == null) {
            return null;
        }
        return tokenData;
    }

    @Override
    public Set<String> getAccessToken(String code, Long id) {
        return userToken(code, id);
    }

    @Override
    public Boolean tokenAuthentication(String token) {
        if (StringUtils.isEmpty(token)) {
            return Boolean.FALSE;
        }
        return jwtTokenFactoryService.tokenAuthentication(token);
    }

    public TokenManageRedisServiceImpl(JwtTokenFactoryServiceImpl jwtTokenFactoryService, RedisService redisService, TokenProperties tokenProperties) {
        this.jwtTokenFactoryService = jwtTokenFactoryService;
        this.redisService = redisService;
        this.tokenProperties = tokenProperties;
    }

    private Set<String> userToken(String code, Long memberId) {
        Set<Object> objects = redisService.hashKeys(this.getUserTokenKey(code, memberId));
        if (CollectionUtils.isEmpty(objects)) {
            return new HashSet<>();
        }
        return objects.stream().filter(Objects::nonNull).map(Object::toString).collect(Collectors.toSet());

    }
}
*/
